<template>
	<!-- 商品仓库 -->
	<scroll-view class="scrollSty" scroll-y @scrolltolower="scrolltolowerFnc">
		<view class="shopStore">
			<view class="shopping-search">
				<uni-search-bar
					radius="100"
					style="flex: 1"
					v-model="shopQuery.keyword"
					placeholder="请输入药品名称、症状、品牌搜索"
					clearButton="always"
					cancelButton="none"
					@confirm="search"
					@cancel="cancel"
				/>
				<text class="search-title" @click="search">搜索</text>
			</view>
			<view class="content">
				<view class="list" v-for="(item, index) in shopListData" :key="index">
					<view class="images">
						<image mode="scaleToFill" :src="imageUrls(item.fileUrl)" class="shopImage"></image>
					</view>
					<view style="box-sizing: border-box; padding: 20rpx">
						<view class="title">{{ item.name || '暂无信息' }}</view>
						<view class="con">
							<uni-tag size="mini" :text="item.mediaName" type="error" class="tags"></uni-tag>
						</view>
						<view class="shop-price">
							<view style="font-size: 26rpx">
								批发价：
								<text class="shop-price-mini">￥</text>
								{{ item.saleprice > 0 ? item.saleprice : item.price }}
							</view>
						</view>
					</view>
					<view class="load_btn">
						<view class="btn" @click="openLoadingShop(item)">导入商品</view>
					</view>
				</view>
			</view>

			<u-popup :show="isShow" @close="getClose" :round="10" mode="bottom" closeable>
				<view class="shopLoadingMain">
					<view>
						<view class="shopLoading-header">补充信息</view>
						<view style="height: 60vh; overflow-y: scroll; box-sizing: border-box; padding: 30rpx">
							<uni-forms>
								<uni-forms-item label="商品分类" required label-width="150rpx">
									<uni-data-select v-model="categoryId" :localdata="categoryList" @change="getChange"></uni-data-select>
								</uni-forms-item>
								<uni-forms-item label="出售价格" required label-width="150rpx">
									<uni-easyinput type="text" v-model="price_value" placeholder="请输入出售价格" />
								</uni-forms-item>
								<uni-forms-item label="商品图片" required label-width="150rpx">
									<uni-file-picker limit="1" :auto-upload="true" v-model="shopImage" fileMediatype="image" mode="grid" @select="select" @delete="delIMG"></uni-file-picker>
								</uni-forms-item>
								
								<!-- <uni-forms-item label="一级分佣" required label-width="150rpx">
									<uni-easyinput type="text" v-model="price_value" placeholder="请输入一级分佣" />
								</uni-forms-item>
								<uni-forms-item label="二级分佣" required label-width="150rpx">
									<uni-easyinput type="text" v-model="price_value" placeholder="请输入二级分佣" />
								</uni-forms-item>
								<uni-forms-item label="缺代理分佣" required label-width="150rpx">
									<uni-easyinput type="text" v-model="price_value" placeholder="请输入缺代理分佣" />
								</uni-forms-item> -->

								<uni-forms-item label="商品详情" required label-width="150rpx"></uni-forms-item>
								<editorComp v-if="isShow" ref="userInfoRef" :value="editorContent" @input="getInput" keyName="editorContent" />
							</uni-forms>
						</view>
					</view>
					<view class="shopLoading-btn" @click="getcloudManageAddGoodsFromPlatform">
						<view class="btn">确认导入</view>
					</view>
				</view>
			</u-popup>
		</view>
	</scroll-view>
</template>

<script>
import { homeGoodsList } from '../../../api/home/index.js';
import { flashManageAddGoods, flashManageCategory } from '../../../api/flashManage/index.js';
import editorComp from '../../../components/editorComp/editorComp.vue';
import { imageUrl } from '../../../utils/tools.js';
import { BASE_URL } from '../../../api/baseurl.js';
export default {
	data() {
		return {
			shopQuery: {
				page: 1,
				limit: 10,
				type: '0',
				keyword: '',
				store_id: 0
			},
			shopListData: [],
			isShow: false,
			price_value: '',
			editorContent: '',
			shopInfo: {},
			categoryId: '',
			categoryList: [],
			shopImage:[],
			tokenValue:""
		};
	},
	async onLoad() {
		this.gethomeGoodsList();
		this.getcloudManageCategory();
		this.tokenValue = (await uni.getStorageSync('token')) || '';
	},
	components: {
		editorComp
	},
	methods: {
		async gethomeGoodsList() {
			try {
				const resData = await homeGoodsList(this.shopQuery);
				let list = resData.data.data || [];
				this.shopListData = [...(this.shopListData || []), ...list];
			} catch (e) {
				throw Error(e);
			}
		},

		// 商品分类
		async getcloudManageCategory() {
			this.storeType = this.$store.state.storeType;
			try {
				const resData = await flashManageCategory({
					type: this.storeType
				});
				const list = resData.data;
				list.forEach((item) => {
					this.categoryList.push({
						value: item.id,
						text: item.name
					});
				});
			} catch (e) {
				throw Error(e);
			}
		},

		getChange(e) {
			this.categoryId = e;
		},

		search() {
			this.shopListData = [];
			this.shopQuery.page = 1;
			this.gethomeGoodsList();
		},

		openLoadingShop(info) {
			this.isShow = true;
			this.shopInfo = info;
			this.$nextTick(() => {
				this.price_value = this.shopInfo.saleprice;
				this.categoryId = this.shopInfo.category_id;
				this.editorContent = this.shopInfo.content;
				if(this.shopInfo.fileUrl){
				this.shopImage = [{
					url: this.shopInfo.fileUrl,
					fullurl: this.shopInfo.url
				}]
				}
			});
		},

		getClose() {
			this.isShow = false;
			this.categoryId = '';
			this.price_value = '';
			this.$nextTick(() => {
				this.editorContent = '';
			});
		},

		imageUrls(url) {
			return imageUrl(url);
		},

		// 导入
		async getcloudManageAddGoodsFromPlatform() {
			if (!this.price_value) {
				return uni.showToast({
					title: '请输入价格',
					icon: 'none'
				});
			}

			if (Number(this.shopInfo.saleprice) >= Number(this.price_value)) {
				return uni.showToast({
					title: '出售价格必须大于批发价',
					icon: 'none'
				});
			}

			if (!this.categoryId) {
				return uni.showToast({
					title: '请选择分类',
					icon: 'none'
				});
			}
			
			if(this.shopImage.length == 0){
				return uni.showToast({
					title: '请选择上传商品图片',
					icon: 'none'
				});
			}
			
			const storeType = this.$store.state.storeType;
			let shopQuery = {
				goods_id: this.shopInfo.id,
				price: this.price_value,
				type: storeType,
				category_id: this.categoryId,
				fileUrl:this.shopImage[0].url
			};

			if (!this.editorContent) {
				shopQuery.content = this.shopInfo.content;
			} else {
				shopQuery.content = this.editorContent;
			}

			try {
				
				const resData = await flashManageAddGoods(shopQuery);
				uni.showToast({
					title: '导入成功',
					icon: 'none'
				});
				this.getClose();
			} catch (e) {
				throw Error(e);
			}
		},

		// 触底
		async scrolltolowerFnc() {
			this.shopQuery.page++;
			await this.gethomeGoodsList();
		},

		getInput(e) {
			const { editorContent } = e;
			this.editorContent = editorContent;
		},
		
		//图片上传
		select(e) {
			let filePath = e.tempFilePaths[0];
			uni.uploadFile({
				url: BASE_URL + 'common/upload',
				filePath: filePath,
				name: 'file',
				header: { token: this.tokenValue },
				success: (uploadFileRes) => {
					let path = JSON.parse(uploadFileRes.data);
					this.shopImage.push({
						url: path.data.fullurl,
						fullurl: path.data.url
					});
				}
			});
		},
		
		//图片删除
		delIMG(e, type) {
			this.shopImage = [];
		},
	}
};
</script>

<style>
page {
	display: flex;
	flex-direction: column;
	height: 100%;
}
</style>

<style scoped lang="scss">
.scrollSty {
	flex: 1;
	overflow: scroll;
}
.shopStore {
	.shopping-search {
		display: flex;
		align-items: center;
		background-color: #fff;
		.search-title {
			margin-right: 20rpx;
			font-size: 28rpx;
		}
	}
	.content {
		box-sizing: border-box;
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
		padding: 20rpx;
		.list {
			width: 48%;
			border: 1px solid #f4f4f4;
			background-color: #fff;
			border-radius: 24rpx;
			overflow: hidden;
			.images {
				box-sizing: border-box;
				padding: 20rpx;
				padding-bottom: 0px;
				.shopImage {
					width: 100%;
					height: 250rpx;
					border-radius: 6rpx;
				}
			}
			.title {
				font-size: 13px;
				font-weight: bold;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				overflow: hidden;
				word-wrap: break-word;
				width: 100%;
				color: #444;
				height: 32px;
			}
			.con {
				margin: 10rpx 0px;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
			.shop-price {
				color: #e43927;
				font-size: 36rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.shop-price-mini {
					font-size: 24rpx;
				}

				.shop-cart {
					background-color: #e43927;
					width: 50rpx;
					height: 50rpx;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}

			.load_btn {
				box-sizing: border-box;
				padding: 20rpx;
				padding-top: 0px;
				.btn {
					background-color: #e43927;
					color: #fff;
					border-radius: 10rpx;
					height: 60rpx;
					line-height: 60rpx;
					text-align: center;
					font-size: 26rpx;
				}
			}
		}
	}

	.shopLoadingMain {
		.shopLoading-header {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			font-size: 28rpx;
			font-weight: bold;
			color: #444;
		}

		.editor {
			width: 100%;
			min-height: 300rpx;
			background-color: #f4f4f4;
			border-radius: 10rpx;
			box-sizing: border-box;
			padding: 20rpx;
		}

		.shopLoading-btn {
			box-sizing: border-box;
			padding: 20rpx;
			.btn {
				background-color: #e43927;
				color: #fff;
				font-size: 28rpx;
				font-weight: bold;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 10rpx;
				text-align: center;
			}
		}
	}
}
</style>
